/**
 * Created by meiguizhinian on 2018/7/23
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//cocos2dx class
ivy.UIParticleBox = ivy.UIBase.extend({
	ctor: function (formName, blockName, sourceControlData) {
		this._super();

		if (formName && blockName && sourceControlData) {
			this.initWith(formName, blockName, sourceControlData, true);
		}
	},

	initWith : function(formName, blockName, sourceControlData, trueType=false) {
		this.init();
		this.setUIElementName(formName, blockName);
		let uim = ivy.UIManager.getInstance();
		let x = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosX);
		let y = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.ePosY);
		let w = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eWidth);
		let h = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eHeight);
		let layerID = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eLayerID);
		this._layerIDInEditor = layerID;

		this.setLocalZOrder(layerID);

		this.setSrcRect(cc.rect(x, y, w, h));
		this.setPosition(ivy.Tools.ConvertPointToGL(cc.p(x + w / 2.0, y + h / 2.0)));
		this.setSrcPosition(this.getPosition());
		this.setContentSize(cc.size(w, h));
		let anchorType = ivy.UIBase.UIAnchorType.eAnchorHV;
		this.setAnchorType(anchorType);
		this._enterActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ParticleBox_EnterAction_action]));
		this._outActionID.setValue(uim.getControlActionDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ParticleBox_OutAction_action]));

		let delaySecond = uim.getControlFloatDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ParticleBox_DelaySecond_float]);
		let psFileName = uim.getControlStringDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ParticleBox_ParticleFilePath_string]);
		this._isAutoReleaseWhenEnd = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ParticleBox_AutoReleaseWhenFinish_list]);
		let positionType = uim.getControlListDataByFindIndex(sourceControlData.propertyDataFindIndex[ivy.DefaultSourceControlTypes.SCT_ELEMENT_ParticleBox_PositionType_list]);
		let isFileExist = cc.FileUtils.getInstance().isFileExist(psFileName);
		if (isFileExist) {
			this._psQuad = new cc.ParticleSystem(psFileName);
			if (this._psQuad) {
				this._psQuad.setPositionType(positionType);
				this.addChild(this._psQuad);
				this._psQuad.setPosition(cc.p());
				if (delaySecond > 0.0) {
					this._psQuad.pauseEmissions();
					this.runAction(cc.sequence(
						cc.delayTime(delaySecond),
						cc.callFunc(function(){
							this._psQuad.resumeEmissions();
							this._psQuad.resetSystem();
						}.bind(this))
					));
				}
				this._psQuad.setAutoRemoveOnFinish(this._isAutoReleaseWhenEnd);
			}
		}
		//////////////////////////////////////////////////////////////////////////
		//设置初始可视性 wsq-2017-3-7
		let isNotVisble = uim.getBlockDataByType(formName, blockName, ivy.UIManager.FormDataType.eNotVisble);
		if (isNotVisble === 1) {
			this.setVisible(false);
		}
	},
	reset : function() {
		if (!this._isAutoReleaseWhenEnd && this._psQuad) {
			this._psQuad.resetSystem();
		}
	},
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////protected:
	_psQuad : null,//cocos2d::ParticleSystemQuad*
	_isAutoReleaseWhenEnd : false,
});

ivy.UIParticleBox.create = function (formName, blockName, sourceControlData) {
	return new ivy.UIParticleBox(formName, blockName, sourceControlData);
};
